package com.test.mooan.labama.activity_fragment_boom.process_boom;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.test.mooan.labama.R;
import com.test.mooan.labama.activity_fragment_boom.base_boom.BaseInfoActivity_boom;
import com.test.mooan.labama.bean_boom.process_boom.ProcessInfo_boom;
import com.test.mooan.labama.utils_boom.helper_boom.UserInfoHelper_boom;
import com.test.mooan.labama.utils_boom.http_boom.HttpManager_boom;
import com.test.mooan.labama.view_boom.widget_boom.TitleView_boom;


public class H5InfoActivity_boom extends BaseInfoActivity_boom {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_h5_info_boom);
        init();
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void init() {
        TitleView_boom titleView = findViewById(R.id.activity_h5_info_view_title);
        setBackBtn_boom(titleView.getBackIb());

        String url = ProcessInfo_boom.getInstance().gH5Url();

        WebView webView = findViewById(R.id.activity_h5_info_view_web);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setTextZoom(100);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        try {
            webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
            webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
            webView.getSettings().setAllowFileAccessFromFileURLs(true);
            webView.getSettings().setAllowContentAccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return shouldOverrideUrlLoading(view, request.getUrl().toString());
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (!TextUtils.isEmpty(url)) {
                    if (url.contains("peratorInformationPage")) {
                        //记录完成埋点
                        UserInfoHelper_boom.gInstance().addEventPoint(pageTag + "CASH_ARROW_CLICK");
                        //保存运营商认证
                        HttpManager_boom.getInstance().requestUserProcess();
                    }
                }
                return false;
            }

            @Override
            public void onLoadResource(WebView view, String url) {
                super.onLoadResource(view, url);
                if (url.contains("inAppH5Success")) HttpManager_boom.getInstance().requestUserProcess();
            }
        });

        findViewById(R.id.activity_h5_info_btn_next).setOnClickListener(v -> {

            UserInfoHelper_boom.gInstance().addEventPoint((pageTag + "_NEXT_CLICK").toUpperCase());

            HttpManager_boom.getInstance().requestUserProcess();
        });

        webView.loadUrl(url);
    }

}